package game.mapviewer.popups 
{
	import com.greensock.TweenLite;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import game.mapviewer.popups.Popup;
	import game.unitmanager.OrderTypes;
	/**
	 * ...
	 * @author rnk
	 */
	public class ArmyOrdersPopup extends Popup
	{
		private var bg:MovieClip;
		private var ResultSendCallback:Function;
		
		public function ArmyOrdersPopup() 
		{
			
		}
		
		override public function Init():void 
		{
			ResultSendCallback = params.callback;
			
			bg = new commands_popup();
			addChild(bg);
			bg.scaleX = bg.scaleY = bg.alpha = 0.0;
			TweenLite.to(bg, 0.5, { scaleX:1.0, scaleY:1.0, alpha:1.0 } );
			
			SetListeners(true);
			
		}
		
		private function SetListeners(orly:Boolean):void 
		{
			var buttons:Array = [bg.march, bg.defend, bg.support, bg.raid];
			for (var i:int = 0; i < buttons.length; i++)
			{
				if (orly)
					buttons[i].addEventListener(MouseEvent.CLICK, OnButtonClick, false, 0, true);
				else
					buttons[i].removeEventListener(MouseEvent.CLICK, OnButtonClick);
			}
		}
		
		private function OnButtonClick(e:MouseEvent):void 
		{
			var result:int = 0;
			switch (e.currentTarget) 
			{
				case bg.march:
					result = OrderTypes.MARCH;
				break;
				case bg.defend:
					result = OrderTypes.DEFEND;
				break;
				case bg.support:
					result = OrderTypes.SUPPORT;
				break;
				case bg.raid:
					result = OrderTypes.RAID;
				break;
			}
			
			StartCloseAnimation(result);
			ResultSendCallback(result);
		}
		
		private function StartCloseAnimation(result:int):void 
		{
			TweenLite.to(bg, 0.5, { scaleX:0.0, scaleY:0.0, alpha:0.0 , onComplete:OnCloseAnimationFinish,onCompleteParams:[result]} );
		}
		
		private function OnCloseAnimationFinish(result:int):void 
		{
			//ResultSendCallback(result);
			Close();
		}
		
		override public function Die():void 
		{
			TweenLite.killTweensOf(this);
			super.Die();
			SetListeners(false);
		}
		
		
	}

}